package com.lemon.support.exception;

import com.lemon.support.base.LoggerAble;
import com.lemon.support.base.TopEnum;
import com.lemon.support.validator.ArgValidator;
import org.apache.commons.lang3.StringUtils;

import static com.lemon.support.constants.PublicConstants.EMPTY_STRING;

/**
 * @program: lemonnote
 * @description: 顶层异常，修饰符：包级
 * @author: Lemontree
 * @create: 2018-05-18 22:29
 **/
public class BaseBizException extends RuntimeException
        implements LoggerAble {

    private static final String DEFAULT_ERROR_MSG = "系统未知错误";
    protected int code;
    protected String message;
    /** 解决方案 */
    protected String solution;
    /** 是否作为前端提示语 ,默认不作为前端提示语 */
    protected boolean visible;

    public BaseBizException(String message) {
        this(ExceptionResCode.SYS_UNEXPECTED_ERROR.getCode(), message);
    }

    public BaseBizException(String message, Object args) {
        this(ExceptionResCode.SYS_UNEXPECTED_ERROR.getCode(), message,
                args);
    }

    public BaseBizException(TopEnum topEnum, String solution) {
        this(topEnum.getCode(), topEnum.getMessage(), solution);
    }

    public BaseBizException(TopEnum topEnum) {
        this(topEnum.getCode(), topEnum.getMessage(), EMPTY_STRING);
    }

    public BaseBizException(int code, String message) {
        this(code, message, EMPTY_STRING);
    }

    public BaseBizException(int code, String message, String solution) {
        this(code, message, solution, null);
    }

    public BaseBizException(TopEnum topEnum, Object... args) {
        this(topEnum, null, args);
    }

    public BaseBizException(TopEnum topEnum, String solution,
            Object... args) {
        this(topEnum.getCode(), topEnum.getMessage(), solution, args);
    }

    public BaseBizException(int code, String message, Object... args) {
        this(code, message, null, args);
    }

    public BaseBizException(int code, String message, String solution,
            Object... args) {
        super(message);
        StringBuilder msg = new StringBuilder();
        this.code = code;
        this.message = message;
        if(!StringUtils.isBlank(solution)) {
            this.message = message + "，问题可能出现情况或解决方案：" + solution;
        }
        if(ArgValidator.anyArgsNull(args)) {
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for(int i = 0;; i++) {
            stringBuilder
                    .append(null == args[i] ? "null" : args[i].toString());
            if(i == args.length - 1) {
                stringBuilder.append("]");
                break;
            }
            stringBuilder.append(",");
        }
        this.message = this.message + " ,入参：" + stringBuilder.toString();
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isVisible() {
        return visible;
    }

    public BaseBizException setVisible(boolean visible) {
        this.visible = visible;
        return this;
    }

    /**
     * @Author: Lemontree
     * @Description: 展示给前端
     * @Date: 16:59 2018/7/21
     */
    public BaseBizException viewAble() {
        setVisible(true);
        return this;
    }

    /**
     * @Author: Lemontree
     * @Description: 不展示刚给前端，仅日志打印
     * @Date: 16:59 2018/7/21
     */
    public BaseBizException unViewAble() {
        setVisible(false);
        return this;
    }

    /**
     * @Author: Lemontree
     * @Description: 前端信息选择
     * @Date: 17:00 2018/7/21
     */
    public String popMessage() {
        return visible ? message : DEFAULT_ERROR_MSG;
    }

}
